// pages/recharge/recharge.js
var util = require('../../utils/request.js');
var globalData = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    focus: false,
    money: '0.00',
    balance: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      balance: globalData.userInfo.balance
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  _setFocus() {
    this.setData({
      focus: true,
      money:''
    })
  },
  // 充值
  toRecharge(evt) {
    var that = this;
    var {
      price
    } = evt.detail.value;

    if (!/(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/.test(price)) {
      wx.showToast({
        title: '充值金额格式不正确',
        icon: 'none'
      })
      return false;
    } else if (price <= 0) {
      wx.showToast({
        title: '充值金额不能为0',
        icon: 'none'
      })
      return false;
    } else {
   
      util.req('/payment/recharge', {
        'memberId': globalData.memberId,
        'amount': price
      }, function(res) {
        if (res.resultCode == 200) {
          var resultContent = res.resultContent;
          console.log(res);
          wx.requestPayment({
            'timeStamp': resultContent.timeStamp + "",
            'nonceStr': resultContent.nonceStr + "",
            'package': resultContent.package,
            'signType': 'MD5',
            'paySign': resultContent.sign,
            'success': function(data) {
              console.log(data);
              var memberId = globalData.memberId;
              console.log(memberId);
              util.req('/member/detail', {
                'memberId': memberId
              }, function(res) {
                console.log(res);
                // 格式化余额，保留小数点后两位
                res.resultContent.balance = (res.resultContent.balance).toString();
                if (res.resultContent.balance.indexOf('.') == -1) {
                  res.resultContent.balance = res.resultContent.balance + '.00';
                } else if (res.resultContent.balance.charAt(res.resultContent.balance.length - 2) == '.') {
                  res.resultContent.balance = res.resultContent.balance + '0'
                }
                globalData.userInfo = res.resultContent;
                that.setData({
                  balance: globalData.userInfo.balance
                })
                wx.redirectTo({
                  url: '/pages/rechargeSuc/rechargeSuc?amount=' + price
                });
              })
            }
          })
        } else {
          wx.showToast({
            title: res.resultMsg,
          })
        }
      })
    }

  }
})